package com.fhzn.boss.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzn.boss.entity.Tag;
import com.fhzn.boss.entity.TagType;
import com.fhzn.boss.entity.WebResponse;
import com.fhzn.boss.enums.Constants;
import com.fhzn.boss.service.TagService;
import com.fhzn.boss.tools.PageInfoUtils;
import com.fhzn.boss.web.converter.TagConverter;
import com.fhzn.boss.web.converter.TagTypeConverter;
import com.fhzn.boss.web.request.*;
import com.fhzn.boss.web.vo.TagVO;
import com.fhzn.commons.toolkit.entity.PageInfo;
import com.fhzn.commons.toolkit.entity.PageRequest;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 老顾
 * @apiNote 字典管理
 * @since 2024/1/22
 */
@RestController
@RequestMapping("/tags")
@RequiredArgsConstructor
public class TagController {

    private final TagService tagService;

    @GetMapping(value = "dicts")
    public WebResponse<PageInfo<TagVO>> dicts(@RequestParam String type,
                                              @RequestParam(required = false) Integer parentId,
                                              @RequestParam(required = false) String name,
                                              PageRequest page) {
        Page<Tag> tagPage = tagService.dictsWithPage(DictQuery.typeOf(type).setPage(page).setParentId(parentId).setIfHidden(false).setName(name));
        return WebResponse.success(PageInfoUtils.convert2page(tagPage, TagConverter::fromTag));
    }


    @GetMapping(value = "dicts/codes")
    public WebResponse<List<TagVO>> dictsByCodes(@RequestParam List<String> codes) {
        List<TagVO> list = Lists.newArrayListWithCapacity(codes.size());
        codes.stream().map(str -> StringUtils.split(str, ':')).forEach(array -> {
            if (array.length != Constants.ARRAY_LENGTH) {
                return;
            }
            Tag dict = tagService.dict(array[0], array[1]);
            if (!Objects.isNull(dict)) {
                list.add(TagConverter.fromTag(dict));
            }
        });
        return WebResponse.success(list);
    }


    @GetMapping(value = "levels")
    public WebResponse<List<TagVO>> levels(@RequestParam String type) {
        List<Tag> tags = tagService.getWithSubTags(type, null);
        return WebResponse.success(tags.stream().map(TagConverter::fromTag).collect(Collectors.toList()));
    }

    @PostMapping(value = "levels/update")
    public WebResponse<Integer> update(@Validated @RequestBody UpdateLevelRequest request) {
        Tag tag = TagConverter.toTag(request);
        tagService.saveOrUpdate(tag);
        return WebResponse.success(tag.getId());
    }

    @PostMapping(value = "types")
    public WebResponse<Integer> saveOrUpdateType(@Validated @RequestBody UpdateTypeDictRequest request) {
        TagType tagType = TagTypeConverter.toTag(request);
        tagService.saveOrUpdateType(tagType);
        return WebResponse.success(tagType.getId());
    }

    @PostMapping(value = "dicts")
    public WebResponse<Integer> update(@Validated @RequestBody UpdateDictRequest request) {
        Tag tag = TagConverter.toTag(request);
        tagService.saveOrUpdate(tag);
        return WebResponse.success(tag.getId());
    }

    @PostMapping(value = "delete")
    public WebResponse<Void> deleteTag(@Validated @RequestBody IdRequest request) {
        tagService.delete(request.getId().intValue());
        return WebResponse.success();
    }

    @GetMapping(value = "dicts/classify/list")
    public WebResponse<List<TagVO>> dictsClassifyList() {
        List<TagType> tagTypeList = tagService.dictsClassifyList();
        return WebResponse.success(tagTypeList.stream().map(TagConverter::fromTagClassify).collect(Collectors.toList()));
    }

    @GetMapping(value = "dicts/classify/appCode/list")
    public WebResponse<List<TagVO>> dictsClassifyAppCodeList(String appCode) {
        List<TagType> tagTypeList = tagService.dictsClassifyAppCodeList(appCode);
        return WebResponse.success(tagTypeList.stream().map(TagConverter::fromTagClassify).collect(Collectors.toList()));
    }

    @GetMapping(value = "dicts/built-in/appCode/list")
    public WebResponse<List<TagVO>> dictsBuiltInAppCodeList(String appCode) {
        List<TagType> tagTypeList = tagService.dictsBuiltInAppCodeList(appCode);
        return WebResponse.success(tagTypeList.stream().map(TagConverter::fromTagClassify).collect(Collectors.toList()));
    }

    @PostMapping(value = "/platform/import")
    public WebResponse<Void> importPlatformDict(@RequestBody String data) {
        tagService.importPlatformDict(data);
        return WebResponse.success();
    }
}
